import {request, handleFetchResultNotification, REQUEST_TYPE} from '../utils';
import {formatMessage} from './intl';
import * as dto from './DTO';
import stringify from 'Shared/utils/stringify';

const baseUrl = '/sales/api/v1';

const getInitList = formatMessage({
    id: 'api.getInitList',
    defaultMessage: '查询初始数据'
});

const getListDesc = formatMessage({
    id: 'api.getList',
    defaultMessage: '查询列表数据'
});

const getDetailDesc = formatMessage({
    id: 'api.getDetail',
    defaultMessage: '查询详情'
});

const newOrderDesc = formatMessage({
    id: 'api.newOrderDesc',
    defaultMessage: '新增'
});

const getConfirmDesc = formatMessage({
    id: 'api.getConfirmDesc',
    defaultMessage: '确认'
});

const exportDesc = formatMessage({
    id: 'api.exportData',
    defaultMessage: '导出'
});

const modifyOrderDesc = formatMessage({
    id: 'api.modifyOrderDesc',
    defaultMessage: '编辑'
});

const getAbandonDesc = formatMessage({
    id: 'api.getAbandonDesc',
    defaultMessage: '作废'
});

const getSalePackingList = formatMessage({
    id: 'api.getSalePackingList',
    defaultMessage: '查询整车包装箱单'
});
// 初始化查询
export const initQuery = () => request(`${baseUrl}/ui/commerceInvoice/init`, null, dto.InitData)
    .then(handleFetchResultNotification(REQUEST_TYPE.initQuery, getInitList));

// 查询列表
export const getList = condition => {
    const queryString = stringify(condition);
    return request(`${baseUrl}/commerceInvoices?${queryString}`, null, dto.List)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getListDesc));
};

// 查询详情
export const getDetail = id => request(`${baseUrl}/commerceInvoices/${id}`, null, dto.Detail)
    .then(handleFetchResultNotification(REQUEST_TYPE.query, getDetailDesc));

// 新增
export const newOrder = data => request(`${baseUrl}/commerceInvoices`, {
    method: 'POST',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, newOrderDesc));

// 编辑
export const modifyOrder = (id, data) => request(`${baseUrl}/commerceInvoices/${id}`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, modifyOrderDesc));

// 确认
export const confirmOrder = (id, data) => request(`${baseUrl}/commerceInvoices/${id}/confirm`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, getConfirmDesc));

// 作废
export const abandonOrder = (id, rowVersion) => request(`${baseUrl}/commerceInvoices/${id}/abandon`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify({
        rowVersion
    })
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, getAbandonDesc));

//导出
export const exportDate = queryCondition => {
    const queryString = stringify(queryCondition);
    return request(`${baseUrl}/commerceInvoices/export?${queryString}`)
        .then(handleFetchResultNotification(REQUEST_TYPE.submit, exportDesc));
};

//整车包装箱单查询
export const salePackingQuery = condition => {
    const queryString = stringify(condition);
    return request(`${baseUrl}/salePackingLists/forCommerceInvoice?${queryString}`, null, null)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getSalePackingList));
};

